package com.yunxi.controller.order;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.yunxi.core.web.ApiResult;
import com.yunxi.core.web.CurrentUser;
import com.yunxi.core.web.LoginInfo;
import com.yunxi.core.web.ResponseUtil;
import com.yunxi.dealer.pojo.PartnerVO;
import com.yunxi.order.pojo.*;
import com.yunxi.order.service.OrderOemManagerDubboService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api(tags = "wholesale:orderOemManager",description = "批售订单管理接口")
@RestController
@RequestMapping("/wholesale/orderOemManager")
public class OrderOemManagerController {
    @Autowired
    private OrderOemManagerDubboService orderOemManagerDubboService;

    @ApiOperation(value = "批售订单管理条件查询")
    @GetMapping(value = "/queryPmsOrderOemForPage")
    public ApiResult<OrderOemRespVO> queryPmsOrderOemForPage(OrderOemReqVO pmsOrderOemVo,@CurrentUser LoginInfo user, int pageNum, int pageSize){
        Object partnerVO = (Object) user.getPartner();
        if(partnerVO != null){
            //
            //表示当前用户是渠道商
            partnerVO = JSONObject.parseObject(JSONObject.toJSONString(partnerVO),PartnerVO.class);
            pmsOrderOemVo.setPartnerCode(((PartnerVO) partnerVO).getPartnerCode());
        }else{
            //
            //表示当前用户是主机厂
            return ResponseUtil.fail(401,"当前用户不是渠道商用户，请使用渠道商账户登录！");
        }

        PageInfo<OrderOemRespVO> resp = orderOemManagerDubboService.queryPmsOrderOemForPage(pmsOrderOemVo,pageNum,pageSize);
        return ResponseUtil.ok(resp);

    }

    @ApiOperation(value = "批售单编辑详情查询")
    @GetMapping(value = "/queryOrderOemDetails")
    public ApiResult<OrderOemDetailsRespVO> queryOrderOemDetails(OrderOemDetailsReqVO vo,@CurrentUser LoginInfo user){
        Object partnerVO = (Object) user.getPartner();
        if(partnerVO != null){
            //
            //表示当前用户是渠道商
            partnerVO = JSONObject.parseObject(JSONObject.toJSONString(partnerVO),PartnerVO.class);
            vo.setPartnerCode(((PartnerVO) partnerVO).getPartnerCode());
        }else{
            //
            //表示当前用户是主机厂
            return ResponseUtil.fail(401,"当前用户不是渠道商用户，请使用渠道商账户登录！");
        }
         OrderOemDetailsRespVO resp = orderOemManagerDubboService.queryOrderOemDetails(vo);
         return ResponseUtil.ok(resp);
    }

    @ApiOperation(value = "批售订单明细查询（已提交）")
    @GetMapping(value = "/queryOrderOemDetailsAlreadySubmitForPage")
    public ApiResult<OrderOemDetailsAlreadySubmitRespVO> queryOrderOemDetailsAlreadySubmitForPage(OrderOemDetailsAlreadySubmitReqVO vo,@CurrentUser LoginInfo user){
        Object partnerVO = (Object) user.getPartner();
        if(partnerVO != null){
            //
            //表示当前用户是渠道商
            partnerVO = JSONObject.parseObject(JSONObject.toJSONString(partnerVO),PartnerVO.class);
            vo.setPartnerCode(((PartnerVO) partnerVO).getPartnerCode());
        }else{
            //
            //表示当前用户是主机厂
            return ResponseUtil.fail(401,"当前用户不是渠道商用户，请使用渠道商账户登录！");
        }
        PageInfo<OrderOemDetailsAlreadySubmitRespVO> resp = orderOemManagerDubboService.queryOrderOemDetailsAlreadySubmitForPage(vo,1,100);
        return ResponseUtil.ok(resp);
    }

    @ApiOperation(value = "批售订单提交")
    @PostMapping(value = "/submitOrderVehicle")
    public ApiResult<Boolean> submitOrderVehicle(@CurrentUser LoginInfo user,@RequestBody OrderVehicleSubmitReqVO vo){
        vo.setUserCode(user.getUserCode());
        boolean isSuccess = orderOemManagerDubboService.submitOrderVehicle(vo);
        return ResponseUtil.ok(isSuccess);
    }

    /**
     * 修改保存批售单信息
     * @param vo
     * @return
     */
    @ApiOperation(value = "批售订单管理修改保存批售单")
    @PostMapping(value = "/updatePmsOrderOem")
    public ApiResult<Boolean> updatePmsOrderOem(@RequestBody OrderOemUpdateReqVO vo){
        boolean isSuccess = orderOemManagerDubboService.updatePmsOrderOem(vo);
        return ResponseUtil.ok(isSuccess);
    }

    @ApiOperation(value = "批售订单管理保存批售单")
    @PostMapping(value = "/savePmsOrderOem")
    public ApiResult<Boolean> savePmsOrderOem(@RequestBody OrderOemSaveReqVO vo, @CurrentUser LoginInfo user){
        Object partnerVO = (Object) user.getPartner();
        if(partnerVO != null){
            //
            //表示当前用户是渠道商
            partnerVO = JSONObject.parseObject(JSONObject.toJSONString(partnerVO),PartnerVO.class);
            vo.setPartnerCode(((PartnerVO) partnerVO).getPartnerCode());
            //vo.setOrderCreateType(0);
        }else{
            //
            //表示当前用户是主机厂
            return ResponseUtil.fail(401,"非渠道商用户无法访问此功能！");
        }

        vo.setCreateBy(user.getUserCode());
        vo.setUserCode(user.getUserCode());
        boolean isSuccess = orderOemManagerDubboService.savePmsOrderOem(vo,user);
        return ResponseUtil.ok(isSuccess);
    }

    @ApiOperation(value = "订单管理主数据查询")
    @GetMapping(value = "/queryOrderForPage")
    public ApiResult<OrderVehicleManagerDetailsRespVO> queryOrderForPage(OrderVehicleManagerDetailsReqVO vo,@CurrentUser LoginInfo user,int pageNum,int pageSize){

        Object partnerVO = (Object) user.getPartner();
        if(partnerVO != null){
            //
            //表示当前用户是渠道商
            partnerVO = JSONObject.parseObject(JSONObject.toJSONString(partnerVO),PartnerVO.class);
            vo.setPartnerCode(((PartnerVO) partnerVO).getPartnerCode());
        }else{
            //
            //表示当前用户是主机厂
            return ResponseUtil.fail(401,"非渠道商用户无法访问此功能！");
        }
        PageInfo<OrderVehicleManagerDetailsRespVO> resp = orderOemManagerDubboService.queryOrderForPage(vo,pageNum,pageSize);
        return ResponseUtil.ok(resp);
    }

    @ApiOperation(value = "订单管理明细数据查询")
    @GetMapping(value = "/queryOrderVehicleDetails")
    public ApiResult<OrderVehicleDetailsRespVO> queryOrderVehicleDetails(OrderVehicleDetailsReqVO vo,@CurrentUser LoginInfo user){

        Object partnerVO = (Object) user.getPartner();
        if(partnerVO != null){
            //
            //表示当前用户是渠道商
            partnerVO = JSONObject.parseObject(JSONObject.toJSONString(partnerVO),PartnerVO.class);
            vo.setPartnerCode(((PartnerVO) partnerVO).getPartnerCode());
        }else{
            //
            //表示当前用户是主机厂
            return ResponseUtil.fail(401,"非渠道商用户无法访问此功能！");
        }
        OrderVehicleDetailsRespVO resp = orderOemManagerDubboService.queryOrderVehicleDetails(vo);
        return ResponseUtil.ok(resp);
    }

    /**
     * 取消订单申请
     * @param vo
     * @param user
     * @return
     */
    @ApiOperation(value = "取消订单")
    @PostMapping(value = "/cancelOrder")
    public ApiResult<Boolean> cancelOrder(@RequestBody OrderCancelReqVO vo,@CurrentUser LoginInfo user){
        vo.setUserCode(user.getUserCode());
        boolean isSuccess = orderOemManagerDubboService.cancelOrderVehicle(vo);
        return ResponseUtil.ok(isSuccess);
    }

    @ApiOperation(value = "订单修改")
    @PostMapping(value = "/updateOrderVehicl")
    public ApiResult<Boolean> updateOrderVehicle(@RequestBody OrderVehiclReqVO vo,@CurrentUser LoginInfo user){
        Object partnerVO = (Object) user.getPartner();
        if(partnerVO == null){
            //
            //表示当前用户是主机厂
            return ResponseUtil.fail(401,"非渠道商用户无法访问此功能！");
        }

        vo.setUserCode(user.getUserCode());
        boolean isSuccess = orderOemManagerDubboService.updateOrderVehicl(vo);
        return ResponseUtil.ok(isSuccess);
    }

    /**
     * 渠道商价格试算
     * @param user
     * @param vo
     * @return
     */
    @ApiOperation(value = "价格试算")
    @PostMapping(value = "/priceTryCompute")
    public ApiResult<PriceTryComputeRespVO> priceTryCompute(@CurrentUser LoginInfo user,@RequestBody  OrderOemSaveReqVO vo){
        Object partnerVO = (Object) user.getPartner();
        if(partnerVO != null){
            //
            //表示当前用户是渠道商
            partnerVO = JSONObject.parseObject(JSONObject.toJSONString(partnerVO),PartnerVO.class);
            vo.setPartnerCode(((PartnerVO) partnerVO).getPartnerCode());
        }else{
            //
            //表示当前用户是主机厂
            return ResponseUtil.fail(401,"非渠道商用户无法访问此功能！");
        }
        PriceTryComputeRespVO tryResult = orderOemManagerDubboService.priceTryCompute(vo);
        return ResponseUtil.ok(tryResult);
    }

    @ApiOperation(value = "渠道商取消订单审核主数据查询")
    @GetMapping(value = "/queryCancelOrderAutitForPage")
    public ApiResult<OrderApproveCancelRespVO> queryCancelOrderAutitForPage(@CurrentUser LoginInfo user,OrderApproveCancelReqVO vo, int pageNum, int pageSize) {
        Object partnerVO = (Object) user.getPartner();
        if(partnerVO != null){
            //
            //表示当前用户是渠道商
            partnerVO = JSONObject.parseObject(JSONObject.toJSONString(partnerVO),PartnerVO.class);
            vo.setPartnerCode(((PartnerVO) partnerVO).getPartnerCode());
        }else{
            //
            //表示当前用户是主机厂
            return ResponseUtil.fail(401,"非渠道商用户无法访问此功能！");
        }
        PageInfo<OrderApproveCancelRespVO> resp = orderOemManagerDubboService.queryCancelOrderApproveForPage(vo, pageNum, pageSize);
        return ResponseUtil.ok(resp);
    }

    /**
     * 渠道商修改订单审核主数据查询
     * @param user
     * @param vo
     * @param pageNum
     * @param pageSize
     * @return
     */
    @ApiOperation(value = "渠道商修改订单审核主数据查询")
    @GetMapping(value = "/queryOrderModifyApplyForPage")
    public ApiResult<OrderModifyApplyRespVO> queryOrderModifyApplyForPage(@CurrentUser LoginInfo user,OrderModifyApplyReqVO vo, int pageNum, int pageSize){
        Object partnerVO = (Object) user.getPartner();
        if(partnerVO != null){
            //
            //表示当前用户是渠道商
            partnerVO = JSONObject.parseObject(JSONObject.toJSONString(partnerVO),PartnerVO.class);
            vo.setPartnerCode(((PartnerVO) partnerVO).getPartnerCode());
        }else{
            //
            //表示当前用户是主机厂
            return ResponseUtil.fail(401,"非渠道商用户无法访问此功能！");
        }
        PageInfo<OrderModifyApplyRespVO> resp = orderOemManagerDubboService.queryOrderModifyApplyForPage(vo, pageNum, pageSize);
        return ResponseUtil.ok(resp);
    }
}

